﻿/**  
 * @Title RegexUtil.java  
 * @Package cn.baidu.util  
 * @Description  "正则表达式工具类"
 * @author "Jerry"  
 */
package cn.qihua.util;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class RegexUtil {
	// 域名
	public static final String REGEX_DOMAIN = "^([\\w-]+\\.)+[\\w-]+(/[\\w-./?%&=]*)?$";
	// 中文
	public static final String REGEX_ZH_CN = "^[\\u4e00-\\u9fa5]{0,}$";
	// IP
	public static final String REGEX_IP = "^((\\d|[1-9]\\d|1\\d\\d|2[0-4]\\d|25[0-5]|[*])\\.){3}(\\d|[1-9]\\d|1\\d\\d|2[0-4]\\d|25[0-5]|[*])$";
	// 手机号码
	public static final String REGEX_PHONE = "^(13[0-9]|14[5|7]|15[0|1|2|3|5|6|7|8|9]|18[0|1|2|3|5|6|7|8|9])\\d{8}$";
	// 电话号码
	public static final String REGEX_TEL = "\\d{3}-\\d{8}|\\d{4}-\\d{7}";
	// EMAIL
	public static final String REGEX_EMAIL = "^\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*$";
	// 日期
	public static final String REGEX_DATE = "^((((1[6-9]|[2-9]\\d)\\d{2})-(0?[13578]|1[02])-(0?[1-9]|[12]\\d|3[01]))|(((1[6-9]|[2-9]\\d)\\d{2})-(0?[13456789]|1[012])-(0?[1-9]|[12]\\d|30))|(((1[6-9]|[2-9]\\d)\\d{2})-0?2-(0?[1-9]|1\\d|2[0-8]))|(((1[6-9]|[2-9]\\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))-0?2-29-))$";

	/**
	 * 获得字符串类型
	 * 
	 * @param str
	 * @return int
	 */
	public static int regex(String str) {
		Pattern pattern = Pattern.compile(REGEX_DOMAIN);
		Matcher matcher = pattern.matcher(str);
		if (matcher.matches()) {
			return 1;
		}

		pattern = Pattern.compile(REGEX_ZH_CN);
		matcher = pattern.matcher(str);
		if (matcher.matches()) {
			return 2;
		}
		return 0;
	}

	/**
	 * 匹配验证字符串
	 * 
	 * @param str
	 *            要验证的字符串
	 * @param strPattern
	 *            要验证的类型
	 * @return Boolean
	 */
	public static Boolean regex(String str, String expression) {
		Pattern pattern = Pattern.compile(expression);
		return pattern.matcher(str).matches();
	}
}
